package com.web.exiaodao.remote.controller;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.exiaodao.remote.service.ApiEBookService;
import com.web.exiaodao.remote.vo.EBookVo;
import com.web.exiaodao.business.entity.EBook;

/**
 * @Title: ApiEBookController.java
 * @Package com.web.exiaodao.remote.controller
 * @Description: 咨询-电子书信息接口
 * @author eason.zt
 * @date 2015年3月23日 下午1:00:00
 * @version V1.0
 */
@Api(value = "EBook", description = "咨询-电子书接口", position = 10)
@Controller
@RequestMapping("/api/eBook")
public class ApiEBookController extends BaseController {

	@Autowired
	private ApiEBookService eBookService;
	
	/**
	 * @Title: findEBookList
	 * @Description: 查询所有咨询-电子书信息
	 * @return    
	 */
	@ApiOperation(value="查询所有咨询-电子书",notes="查询所有咨询-电子书信息,当返回的code=1时，取出object进行显示,存放为数组对象")
	@ResponseBody
	@RequestMapping(value="findEBookList",method=RequestMethod.POST)
	public XaResult<List<EBookVo>> findEBookList(
		@ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
		@ApiParam("页长,字段名:pageSize,默认10") @RequestParam(defaultValue = "10") Integer pageSize
	) throws BusinessException{
		XaResult<List<EBookVo>> xr = new XaResult<List<EBookVo>>();
		try{
			Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, "[{property:'createTime',direction:'DESC'}]");
			Map<String , Object> filterParams =  WebUitl.getParametersStartingWith("{}", "search_");
			return eBookService.findListEQStatusByFilter(XaConstant.Status.valid, filterParams, pageable);
		}catch(Exception e){
			if (e instanceof  BusinessException) {
				xr.error(e.getMessage());
			} else {
				xr.error(XaConstant.Message.error);
			}
		}
		return xr;
	}
	
	/**
	 * @Title: findEBookById
	 * @Description: 根据ID查找单条实体
	 * @param modelId
	 * @return    
	 */
	@ApiOperation(value="根据tId查询咨询-电子书",notes="查询咨询-电子书详细信息,当返回的code=1时，取出object进行显示,存放为单体对象")
	@ResponseBody
	@RequestMapping(value="findEBookById",method=RequestMethod.POST)
	public XaResult<EBookVo> findEBookById(
		@ApiParam("编号,字段名:modelId,必填") @RequestParam(value = "modelId") Long modelId
	) throws BusinessException{
		XaResult<EBookVo> xr = new XaResult<EBookVo>();
		try{
			return eBookService.findOne(modelId);
		}catch(Exception e){
			xr.error(XaConstant.Message.error);
		}
		return xr;
	}
	
	/**
	 * @Title: save
	 * @Description: 新增一条实体
	 * @param userId
	 * @param model
	 * @return    
	 */
	@ApiOperation(value="新增咨询-电子书",notes="供前端前期填充数据测试使用,上线后删除;新增咨询-电子书,当返回的code=1时，保存成功后object返回对象")
	@ResponseBody
	@RequestMapping(value="saveEBook",method=RequestMethod.POST)
	public XaResult<EBookVo> saveEBook(
		@ApiParam("标题,字段名:title") @RequestParam(value = "title") String title,
		@ApiParam("来源,字段名:source") @RequestParam(value = "source") String source,
		@ApiParam("电子书路径,字段名:eBookUrl") @RequestParam(value = "eBookUrl") String eBookUrl,
		@ApiParam("缩略图,字段名:cover") @RequestParam(value = "cover") String cover,
		HttpServletRequest request
	) throws BusinessException{
		EBook model = new EBook();
		model.setTitle(title);
		model.setSource(source);
		model.setEBookUrl(eBookUrl);
		model.setCover(cover);
		return eBookService.createModel(model);
	}
	
	/**
	 * @Title: 
	 * @Description: 修改一条实体
	 * @param userId
	 * @param model
	 * @return    
	 */
	@ApiOperation(value="修改咨询-电子书",notes="修改咨询-电子书,当返回的code=1时，保存成功后object返回对象")
	@ResponseBody
	@RequestMapping(value="updateEBook",method=RequestMethod.POST)
	public XaResult<EBookVo> updateEBook(
		@ApiParam("标题,字段名:title") @RequestParam(value = "title") String title,
		@ApiParam("来源,字段名:source") @RequestParam(value = "source") String source,
		@ApiParam("电子书路径,字段名:eBookUrl") @RequestParam(value = "eBookUrl") String eBookUrl,
		@ApiParam("缩略图,字段名:cover") @RequestParam(value = "cover") String cover,
		@ApiParam("唯一编号,字段名:tId") @RequestParam(value = "tId") Long tId,
	HttpServletRequest request
	) throws BusinessException{
		EBook model = new EBook();
		model.setTitle(title);
		model.setSource(source);
		model.setEBookUrl(eBookUrl);
		model.setCover(cover);
		model.setId(tId);
		return eBookService.updateModel(model);
	}
	
	/**
	 * @Title: operateEBookById
	 * @Description: 操作一个实体状态,根据status进行操作
	 * @param modelId
	 * @param status 		操作类型:-1锁定,0无效,1正常,2发布,3删除,默认删除操作
	 * @return    
	 */
	@ApiOperation(value="根据ID修改咨询-电子书状态",notes="修改咨询-电子书状态,当返回的code=1时，保存成功后object返回对象")
	@ResponseBody
	@RequestMapping(value="operateEBookById",method=RequestMethod.POST)
	public XaResult<EBookVo> operateEBookById(
		HttpServletRequest request,
		@ApiParam("编号,字段名:modelIds,必填") @RequestParam(value = "modelId") String modelIds,
		@ApiParam("操作类型,字段名:status,-1锁定,0无效,1正常,2发布,3删除,选填,默认删除操作") @RequestParam(defaultValue = "3") Integer status
	) throws BusinessException{
		return eBookService.multiOperate(modelIds,status);
	}
}

